package ArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User：Lenovo
 * Date:2025-01-04
 * Time:9:57
 */
//每个玩家的手牌
    class HandCard{
        private ArrayList<Card> cards;
}
//这个类对象表示一张扑克牌
class Card {
    private String suit;//表示花色
    private String rank;//表示点数
    public Card(String suit, String rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }
    public String toString() {
        return "("+this.suit+" "+this.rank+ ")";
    }
}
public class demo1 {
    public static void main(String[] args) {
        //表示一副扑克牌
        ArrayList<Card> deck=createDeck();
        System.out.println("原始的牌:" +deck);
       // Collections.shuffle(deck);
        shuffle(deck);
        System.out.println("洗牌之后："+deck);
        //发牌，假设有三个人，每个人五张牌
        ArrayList<ArrayList<Card>> hands=new ArrayList<ArrayList<Card>>();
        //ArrayList<HandCard> handCards=new ArrayList<HandCard>();
        //先创建三个人手里的牌ArrayList
        for(int i=0;i<3;i++){
            ArrayList<Card> hand=new ArrayList<Card>();
            hands.add(hand);
        }
        for(int i=0;i<5;i++){
            for(int j=0;j<3;j++){
                //发牌是轮流的过程
                ArrayList<Card> currentHand=hands.get(j);
                Card card=deck.remove(0);
                currentHand.add(card);
            }
        }
        //打印每个人的首牌
        for(int i=0;i<3;i++){
            System.out.println("第"+i+"个人的牌"+hands.get(i));
        }
    }
    //自己实现洗牌
    public static void shuffle(ArrayList<Card> deck){
        //把整个ArrayList从后往前遍历
        //针对每一次取到的一张牌，就生成一个随机下标，拿到当前位置的牌和随机位置下标的牌，进行交换。
        Random random=new Random();
        for(int i=deck.size()-1;i>0;i--){
            int j= random.nextInt(deck.size());
            //交换两张牌位置
            Card tmp=new Card(deck.get(i).getSuit(),deck.get(i).getRank());
            deck.set(i,deck.get(j));
            deck.set(j,tmp);
        }
    }
        public static ArrayList<Card> createDeck(){
        //创建一副扑克牌
        ArrayList<Card> deck=new ArrayList<>();
        String[] suits={"♥","♣","♦","♠"};
            for(String suit:suits){
            //在每个花色中生成对应点数的牌
            for(int i=2;i<=10;i++){
                Card card = new Card(suit,""+i);
                deck.add(card);
            }
            Card cardJ=new Card(suit,"J");
            Card cardQ=new Card(suit,"Q");
            Card cardK=new Card(suit,"K");
            Card cardA=new Card(suit,"A");
            deck.add(cardJ);
            deck.add(cardQ);
            deck.add(cardK);
            deck.add(cardA);
        }
           return deck;
        }

}
